import { useState, useEffect, useMemo } from 'react';
import { Alert, Form, Input, message } from 'antd';
import CommonModal from '@/components/CommonModal';
import { union } from 'lodash';
import { getCurrentUser } from '@/utils/authority';

const labelProps = {
    'logisticsNo': '快递单号',
    'logisticsNos': '物流单号',
    'buyerNickName': '买家昵称',
    'uniqueCode': '商品唯一码',
    'selectOutOrderSns': '店铺订单号',
    'selectOrderSns': '系统订单号',
    'uniqueCodeList': '商品唯一码',
    'outOrderSnList': '店铺订单号',
    'orderSnList': '仓库订单号',
    'omsOrderSnList': 'oms系统订单号',
    'logisticsNoList': '物流单号',
    'uniqueCodes': '商品唯一码',
    'omsOrderSns': '系统订单号',
    'orderSns': '系统订单号',
    'orderSns2': '仓库订单号',
    'outOrderSns': '店铺订单号',
    'platRefundSn': '平台退款单号',
    'outOrderSn': '店铺订单号',
    'sourceOrderNo':'奇门外部平台源单号',
    'qmErpSn':'奇门外部erp单号',
    'tcOrderId':'直发分销单号',
    'headCodes': '无头编码',
		'omsOrderSnStr': '系统订单号',
    'qmErpSns': '外部erp单号',
    'orderSnStr': '仓库订单号',
    'uniqueCodesStr': '商品唯一码',
    'goodsCodes': '商品编码',
		'outOrderSnStr': '店铺订单号',
}

//当前参数支持数量
const labelCount = {
	'omsOrderSnStr': 500,
    'qmErpSns': 500,
	'orderSnStr': 500,
	'outOrderSnStr': 2000,
}

export default ({ searchVisible, batchSearchLabel, specLabelList = [], specCountMaps = {}, handleCancel, handleOk }) => {
    const [form] = Form.useForm();
    const [inputValue, setValue] = useState(()=>{
        let str = '';
        // for(let i=0;i<2000;i++){
        //     str += `${i ? ',' : ''}M20240422200004838840${i}`
        // }
        return str
    });

	const maxCount = useMemo(()=>{
        return labelCount[batchSearchLabel] || specCountMaps[batchSearchLabel] || 50;
        const limitMaxCount = labelCount[batchSearchLabel];
        if(limitMaxCount) return limitMaxCount;
        const isDuShang = getCurrentUser()?.uuid == 300107;
        if(isDuShang && specLabelList?.includes(batchSearchLabel)){//杜尚固定字段
          return 2000;
        }else{
          return 50;
        }
    }, [])

    useEffect(()=>{
      form.setFieldsValue({
        orderSn:inputValue
      })
    }, [inputValue])

    const batchSearchOk = () => {
        form.validateFields().then(val => {
            const { orderSn } = val;
            const formatStr = orderSn.replace(/(,|，)+/g, '\n').replace(/(\w)-(\d+)-(\d+)-/g, '$1'),
                  arr = union(formatStr.split('\n')).filter(item=>item.trim());
            if (arr.length > maxCount) {
                message.info(`最多查询${maxCount}条信息`);
                return
            }
            // console.log(arr.join(','))
            // return;
            handleOk(arr.join(','));
            form.resetFields();
        })
    }

    const handleInputAndFormat = (value) => {
        const formatStr = value.replace(/(\w)-(\d+)-(\d+)-/g, '$1');
        setValue(formatStr);
    }

    const messageStatic = useMemo(() => {
        return (<div style={{ padding: "2px 0" }}>
            <div className='flex'>
                <span>1.</span>
                <span style={{ flex: 1, marginLeft: '4px' }}>分行输入内容，提交后主页面输入框会按照逗号自动隔开，点击查询可批量查询；</span>
            </div>
            <div>
                <div className='flex'>
                    <span>2.</span>
                    <div className="flex-column" style={{ flex: 1, marginLeft: '4px' }}>
                        <span>注释: 每行输入1个内容，最多{maxCount}个，例如:</span>
                        <span>1272572508553111112223</span>
                        <span>1272572512332111112226</span>
                    </div>
                </div>
            </div>
        </div>)
    }, [])

    return (
        <CommonModal
            title="批量输入"
            visible={searchVisible}
            onOk={batchSearchOk}
            onCancel={handleCancel}
        >
            <Alert message={messageStatic} style={{ marginBottom: "12px" }} />
            <Form form={form}>
                <Form.Item label={labelProps[batchSearchLabel]} name='orderSn'
                    rules={[{ required: true, message: `请输入${labelProps[batchSearchLabel]}` }]}
                >
                    <Input.TextArea
                       placeholder='请输入'
                       style={{ height: 286 }}
                       value={inputValue}
                       onChange={(e)=>handleInputAndFormat(e.target.value)}
                    />
                </Form.Item>
            </Form>
        </CommonModal>
    )
}